package com.nanfang51g3.ckz.ui.registered;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nanfang51g3.ckz.R;
import com.nanfang51g3.ckz.application.CkzApplication;
import com.nanfang51g3.ckz.ui.base.IPresenter;
import com.nanfang51g3.ckz.ui.base.Base2Activity;
import com.nanfang51g3.ckz.ui.login.LoginActivity;
import com.nanfang51g3.ckz.utils.AppManager;
import com.nanfang51g3.ckz.utils.EasyToast;
import com.nanfang51g3.ckz.utils.KeyBoardUtils;
import com.nanfang51g3.ckz.widget.ClearEditText;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by Administrator on 2016/4/13.
 */
public class Registered3Activity extends Base2Activity implements RegisteredContract.View {

    RegisteredContract.RegisteredActionsListener registeredActionsListener;
    @Bind(R.id.index_tv_title)
    TextView indexTvTitle;
    @Bind(R.id.index_iv_right)
    ImageView indexIvRight;
    @Bind(R.id.id_toolbar)
    Toolbar idToolbar;
    @Bind(R.id.iv_registered_password)
    ImageView ivRegisteredPassword;
    @Bind(R.id.cet_registered_password)
    ClearEditText cetRegisteredPassword;
    @Bind(R.id.iv_registered_confirm_password)
    ImageView ivRegisteredConfirmPassword;
    @Bind(R.id.cet_registered_confirm_password)
    ClearEditText cetRegisteredConfirmPassword;
    @Bind(R.id.iv_registered_id)
    ImageView ivRegisteredId;
    @Bind(R.id.cet_registered_id)
    ClearEditText cetRegisteredId;
    @Bind(R.id.iv_registered_select)
    ImageView ivRegisteredSelect;
    @Bind(R.id.iv_registered_none_select)
    ImageView ivRegisteredNoneSelect;
    @Bind(R.id.tv_keep_password)
    TextView tvKeepPassword;
    @Bind(R.id.relative_iii)
    RelativeLayout relativeIii;
    @Bind(R.id.tv_register)
    TextView tvRegister;
    boolean selectPasswordBoolean = false;
    private String phone;
    @Bind(R.id.index_tv_right_title)
    TextView indexTvRightTitle;

    @Override
    public IPresenter getPresenter() {
        return null;
    }
    @Override
    public int getContentView() {
        return R.layout.registered3;
    }

    @Override
    public boolean isShowLeftIcon() {
        return true;
    }

    @Override
    public boolean isShowRightIcon() {
        return false;
    }

    @Override
    public int setLeftIcon() {
        return R.mipmap.ic_accounting_left;
    }

    @Override
    public int setRightIcon() {
        return 0;
    }

    @Override
    public int showTitle() {
        return R.string.str_registered;
    }

    @Override
    public void onClickLeft(View v) {
        AppManager.getAppManager().finishActivity(this);
    }

    @Override
    public void onClickRight(View v) {

    }

    @Override
    public void showProgress() {
        super.showPro(R.string.str_progress_login);
    }

    @Override
    public void dismissProgress() {
        super.dismissPro();
    }

    @Override
    public void startShowView() {
        startActivity(new Intent(this, LoginActivity.class));
        AppManager.getAppManager().finishActivity(this);
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void showMessage(String message) {
        EasyToast.showShort(this, message);
    }

    @Override
    public void onIntent() {
        super.onIntent();
        phone = getIntent().getStringExtra("phone");
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        registeredActionsListener = DaggerRegisteredContractComponent
                .builder()
                .appComponent(CkzApplication.getAppComponent())
                .registeredPresenterModule(new RegisteredPresenterModule(this))
                .build().getRegisteredPresenter();
        Log.i("TAG", registeredActionsListener == null ? "registeredActionsListener==null" : "registeredActionsListener!=null");
        ButterKnife.bind(this);


    }

    @OnClick(R.id.iv_registered_select)
    public void iv_registered_select(View view) {
        selectPasswordBoolean = false;
        ivRegisteredNoneSelect.setVisibility(View.VISIBLE);
        ivRegisteredSelect.setVisibility(View.INVISIBLE);

    }

    @OnClick(R.id.iv_registered_none_select)
    public void iv_registered_none_select(View view) {
        selectPasswordBoolean = true;
        ivRegisteredNoneSelect.setVisibility(View.INVISIBLE);
        ivRegisteredSelect.setVisibility(View.VISIBLE);
    }


    @OnClick(R.id.tv_register)
    public void tv_register(View view) {
        String password = cetRegisteredPassword.getText().toString().trim();
        String confirmPassword = cetRegisteredConfirmPassword.getText().toString().trim();
        String id = cetRegisteredId.getText().toString().trim();
        KeyBoardUtils.closeKeybord(cetRegisteredId, this);
        /**
         * @see RegisteredPresenter#register(boolean, String, String, String, String)
         */
        registeredActionsListener.register(selectPasswordBoolean, phone, password, confirmPassword, id);

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);//解除绑定，官方文档只对fragment做了解绑
    }
}
